<?php
namespace App\Services;

use App\Traits\ReturnTrait;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\SimpleCache\CacheInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;

class AliService
{
    use ReturnTrait;

    public $accessKeyId;
    public $accessSecret;
    public $client;

    public function __construct()
    {
        $this->accessKeyId = config('ali.AccessKeyID');
        $this->accessSecret = config('ali.AccessKeySecret');
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $this->accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $this->accessSecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        $this->client = new Dysmsapi($config);
    }

    public function sendSms($params){
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => $params['mobile'],
            "signName" => "学法减分快搜",
            "templateCode" => $params['template'],
            "templateParam" => $params['template_params']
        ]);
        // 复制代码运行请自行打印 API 的返回值
        $result = $this->client->sendSms($sendSmsRequest);
        return $result;
    }
}